home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / documents / Inventor / www / workarounds / TextureLeak.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  2.7 KB  |  78 lines

  1. //
  2. // IRIS GL display lists allocated by Texture2 nodes are never freed,
  3. // causing memory to be lost.  This is only a major problem for
  4. // applications that are constantly reading in new textures, either by
  5. // deleting and recreating Texture2 nodes with different filenames, or
  6. // by changing the filename field of the Texture2 node.
  7. //
  8. // This code should be called with the old filename when the Texture2
  9. // node is deleted or changed.  It will look through the internal list
  10. // of files the Texture2 node keeps and remove the texture from its
  11. // list and free up the memory held by the GL.  It may also be used if
  12. // you want to force the Texture2 node to re-read an image file
  13. // (because it changed, for example).
  14. //
  15.  
  16. #include <gl/gl.h>
  17. #include <Inventor/nodes/SoTexture2.h>
  18.  
  19. // Get access to private stuff:
  20. #define private public
  21. #include <Inventor/bundles/SoTextureBundle.h>
  22. #undef private
  23.  
  24. //
  25. // This routine searches through the texture maps maintained by the
  26. // Texture2 node, finds the map with the given filename, and gets rid
  27. // of it.  Because of the way Texture2 is implemented in Inventor 1,
  28. // "getting rid of it" is a little complicated.
  29. //
  30. // First, we can't just remove the map from the linked list because
  31. // Texture2 assumes that the GL texture ID for the last map on the
  32. // list is 2000, the second to last is 2001, etc.  If we created a gap
  33. // in the list then the Texture2 node would re-use a GL texture ID
  34. // that is already in use.
  35. //
  36. // Second, there is no IRIS GL command to delete a texture-- the best
  37. // we can do is redefine a texture to be a 1 by 1 texture that uses
  38. // minimal memory (freeing up whatever memory the old definition
  39. // used).
  40. //
  41. // So, this routine finds the map, marks it by naming it "_UNUSED_",
  42. // and redefines it in GL to be a 1 by 1 texture.
  43. //
  44. // Then, if texture at the head of the list is named "_UNUSED_", we
  45. // really do remove it from the linked list (and the Texture2 node
  46. // will recycle the associated GL texture ID the next time it creates
  47. // a texture).
  48. //
  49. static void
  50. deleteTexture(const char* imageName)
  51. {
  52.     if( (imageName == NULL) || (strcmp( imageName, "") == 0) )
  53.         return;
  54.  
  55.     // delete old map 
  56.     textureMap* map = SoTextureBundle::maps;
  57.     while( map)  {
  58.     if( map->filename == imageName)  {
  59.         // free up texture memory used by  map->index
  60.         static float props[1]; props[0] = TX_NULL;
  61.         texdef2d( map->index, 1, 1, 1, (unsigned long*) props, 
  62.               1, (float*) props);
  63.         map->filename = SbName("_UNUSED_");
  64.         break;
  65.     }
  66.     map = map->next;
  67.     }
  68.     // Now, delete all textures name "_UNUSED_" from the head of the
  69.     // list:
  70.     map = SoTextureBundle::maps;
  71.     while (map && map->filename == SbName("_UNUSED_")) {
  72.     SoTextureBundle::maps = map->next;
  73.  
  74.     delete map;
  75.     map = SoTextureBundle::maps;
  76.     }
  77. }
  78.